﻿namespace BrowserGameFramework.DBConnector.SqlServer
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.SqlClient;
    using BrowserGameFramework.Common.Logging;

    /// <summary>
    /// Sql helper provides some stuff for sql server
    /// </summary>
    public static class SqlHelper
    {
        #region parameter extensions
        /// <summary>
        /// Builds an input parameter with dbtype string
        /// </summary>
        /// <param name="name">Name of parameter</param>
        /// <param name="value">value of parameter</param>
        /// <returns>SQL Parameter object</returns>
        public static SqlParameter BuildInputStringParam(this string value, string name)
        {
            SqlParameter param = new SqlParameter(name, value);
            param.Direction = System.Data.ParameterDirection.Input;
            param.SqlDbType = System.Data.SqlDbType.NVarChar;

            return param;
        }

        /// <summary>
        /// Builds an input parameter with dbtype int32
        /// </summary>
        /// <param name="value">Value of parameter</param>
        /// <param name="name">Name of parameter</param>
        /// <returns>SQL Parameter object</returns>
        public static SqlParameter BuildInputInt32Param(this Int32 value, string name)
        {
            SqlParameter param = new SqlParameter(name, value);
            param.Direction = System.Data.ParameterDirection.Input;
            param.SqlDbType = System.Data.SqlDbType.Int;

            return param;
        }

        /// <summary>
        /// Builds an input parameter with dbtype DateTime
        /// </summary>
        /// <param name="value">Value of parameter</param>
        /// <param name="name">Name of parameter</param>
        /// <returns></returns>
        public static SqlParameter BuildInputDateTimeParam(this DateTime value, string name)
        {
            SqlParameter param = new SqlParameter(name, value);
            param.Direction = System.Data.ParameterDirection.Input;
            param.SqlDbType = System.Data.SqlDbType.DateTime;

            return param;
        }

        #endregion parameter extensions

        /// <summary>
        /// Dump the SqlParameter to the Logfile
        /// </summary>
        /// <param name="cmd"></param>
        public static void DumpSqlParamToLogfile(this SqlCommand cmd)
        {
            Trace.TraceMessage(string.Format("Dumping SQLCommand {0}", cmd.CommandText), Common.Enums.TraceLevel.Information);

            if (cmd.Parameters.Count == 0)
            {
                Trace.TraceMessage(string.Format("The Command {0} has zero parameters", cmd.CommandText), Common.Enums.TraceLevel.Warning);
            }

            foreach (SqlParameter param in cmd.Parameters)
            {
                string value = string.Empty;

                Convert.ToString(param.Value);

                Trace.TraceMessage(string.Format("{0} has value '{1}',is of type '{2}' and the direction is  '{3}' ", param.ParameterName, value, param.TypeName, param.Direction.ToString()), Common.Enums.TraceLevel.Verbose);
            }

            Trace.TraceMessage(string.Format("Dumping SQLCommand {0} complete", cmd.CommandText), Common.Enums.TraceLevel.Information);
        }
    }
}
